package com.enterprise.projectManager.resource;

import com.enterprise.projectManager.dto.LoginRequest;
import com.enterprise.projectManager.dto.SignUpRequest;
import com.enterprise.projectManager.model.Response;
import com.enterprise.projectManager.service.implemention.AccountServiceImpl;
import com.fasterxml.jackson.databind.jsontype.impl.StdTypeResolverBuilder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import static java.time.LocalDateTime.now;
import static java.util.Map.of;
import static org.springframework.http.HttpStatus.OK;

@RestController
@RequiredArgsConstructor
@RequestMapping("/crebas/account")
@Slf4j
public class AccountResource {
    private final AccountServiceImpl accountServiceImpl;
    @GetMapping("/test")
    public ResponseEntity<String> test()
    {
        return ResponseEntity.ok("it's a account test");
    }

    @PostMapping("/login")
    public ResponseEntity<Response> login(@RequestBody LoginRequest loginRequest)
    {
        if (accountServiceImpl.login(loginRequest) == null)
        {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("login",false))
                            .message("登陆失败，请检查账号或密码")
                            .status(HttpStatus.UNAUTHORIZED)
                            .statusCode(HttpStatus.UNAUTHORIZED.value())
                            .build()
            );
        }
        else
        {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("login",accountServiceImpl.login(loginRequest)))
                            .message("登录成功")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        }
    }
    @PostMapping("/signup")
    public ResponseEntity<Response> signup(@RequestBody SignUpRequest signUpRequest)
    {
        if (accountServiceImpl.isExistAccount(signUpRequest.getAccount()))
        {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("signup",false))
                            .message("注册失败，账号已存在")
                            .status(HttpStatus.CONFLICT)
                            .statusCode(HttpStatus.CONFLICT.value())
                            .build()
            );
        } else
        {
            System.out.println(signUpRequest);
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("signup",accountServiceImpl.create(signUpRequest)))
                            .message("注册成功")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        }
    }

    @GetMapping("/listAll")
    public ResponseEntity<Response> listAll()
    {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("users",accountServiceImpl.listAll()))
                        .message("列举所有用户")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }
}
